package cn.bandeqing.productType.rpc.impl;

import cn.bandeqing.base.resp.Resp;
import cn.bandeqing.base.resp.Table;
import cn.bandeqing.frame.exception.CodeException;
import cn.bandeqing.productType.entity.ProductType;
import cn.bandeqing.productType.req.ProductTypeSaveOrUpdateReq;
import cn.bandeqing.productType.req.ProductTypeTableReq;
import cn.bandeqing.productType.resp.ProductTypeTable;
import cn.bandeqing.productType.rpc.IProductTypeRpc;
import cn.bandeqing.productType.service.IProductTypeService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.github.linpeilie.Converter;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboService;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.text.StrUtil;

import static cn.bandeqing.productType.entity.table.ProductTypeTableDef.PRODUCT_TYPE;

@DubboService
public class ProductTypeRpcImpl implements IProductTypeRpc {

    @Resource
    private IProductTypeService productTypeService;
    @Resource
    private Converter converter;

    @Override
    public Resp<Table<ProductTypeTable>> table(ProductTypeTableReq req) {
        QueryWrapper qw = QueryWrapper.create()
                .where(PRODUCT_TYPE.PT_ID.eq(req.getPtId()))
                .where(PRODUCT_TYPE.PT_NAME.like(req.getPtName(), StrUtil::isNotBlank))
                .orderBy(PRODUCT_TYPE.PT_ID.desc());

        Page<ProductTypeTable> page = productTypeService.pageAs(Page.of(req.getCurrent(), req.getPageSize()), qw, ProductTypeTable.class);
        return Resp.success(Table.of(page.getRecords(), page.getTotalRow()));
    }

    @Override
    public void saveOrUpdate(ProductTypeSaveOrUpdateReq req) {
        ProductType pt = converter.convert(req, ProductType.class);
        boolean b = productTypeService.saveOrUpdate(pt);
        Assert.isTrue(b, () -> new CodeException("productTypeService.saveOrUpdate(pt)"));
    }

}
